<template>
	<view style="height: 100%;">
	        <view class="write_wrap">   
	               <view class="uni-form-item uni-column" >
						<view v-if="label1==''">
							<view>
								<text style="font-size: 35rpx;">封面图片：</text>
							</view>
							<view>
								<image class="uploadImg" @click="uploadImg"  src="../../static/pic_upload.png"></image>
							</view>
						</view>
						 <view v-else>
							 <image class="labelImg"  :src="label1"></image>
							<!-- <icon class='imgCancel' type="clear" size='25' data-index="{{item.id}}" catchtap="deleteImg"></icon> -->
							 <view style="" @click="deleteImg()">x</view>
						 </view>
						<view>
							<text class="adviseText"> 优质的封面有利于推荐 </text>
						</view>
					</view>
					<view class="uni-form-item uni-column">
					    <input class="title" cursor-spacing="2" :adjust-position="adjust" v-model="title" placeholder="请输入标题" />
					</view>
					<view >
						<scroll-view style="height: 80%;bottom: var(--window-bottom);" scroll-y="true">
							<jinEdit ref="jinEditRef" @publish="formSubmit" placeholder="请输入正文" @editOk="editOk" :header="{'authorization':authorization}" uploadFileUrl="uploadUrl"></jinEdit>
						</scroll-view>
					</view>
			
	        </view>
	    </view>
</template>

<script>
	import jinEdit from '../../components/jin-edit/jin-edit.vue';
	var loginRes;
	export default {
		components: {
		   jinEdit
		},
		
		data() {
			return {
				placeholder: '请输入文章内容...',
				label1:'',
				title:'',
				content:'',
				name:'',
				authorization:'',
				classify1:'',
				adjust:false,
				uploadUrl:uni.$http.baseUrl+'/file_upload/upload'
			};
		},
		methods:{
		
			// 点击发布
			editOk(res) {
			    console.log('editOk(res)----',res);
				this.content = res.html;
				this.name = res.text;
				console.log('this.content = res.html',this.content);
			},
			deleteImg(){
				this.label1='';
			},
			uploadImg(){
				uni.chooseImage({
					count:1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success: (res) => {
						uni.showLoading({
							title:'正在上传...'
						})
						uni.uploadFile({
						            url: uni.$http.baseUrl+'/file_upload/upload', //仅为示例，非真实的接口地址
						            filePath: res.tempFilePaths[0],
						            name: 'file',
									header:{authorization:uni.getStorageSync('authorization')},
						            success: (uploadFileRes) => {
										uni.hideLoading();
										this.label1 = JSON.parse(uploadFileRes.data).fileUrl;
										console.log('-------this.label1-------',this.label1);
										
						            },
									fail:(res)=>{
										uni.hideLoading();
									}
						});
					}
				})
			},
			setValueGroup(e){
				this.classify1 = e.detail.value;
			},
			
			formSubmit() {
					  if(this.title==''||this.title==undefined){
						  uni.showToast({
							title:'文章标题不能为空！'
						  })
						  return false
					  }
					  if(this.content==''||this.content==undefined){
						  uni.showToast({
							title:'文章内容不能为空！'
						  })
						  return false
					  }
					  if(this.classify1==''||this.classify1==undefined){
						  uni.showToast({
							title:'文章类型不能为空！'
						  })
						  return false
					  }
					 //  if(this.label1==undefined||this.label1==''){
						// uni.showToast({
						// 	title:'请上传封面图片！'
						// })
						// return false
					 //  }
					  
					let formdata = {
						title:this.title,
						content:this.content,
						name:this.name,
						classify1:this.classify1,
						label1:this.label1
					};
					this.saveBlog(formdata);
				},
						
						clearInfo(classify1){
							this.title='';
							console.log('console.log(th',this.content)
							if(this.content!=''){
								console.log(this.content!='')
								this.$refs.jinEditRef.clear();
							}
							this.content='';
							this.name='';
							this.classify1=classify1;
							this.label1='';
							
						},
						
						async saveBlog(formdata) {
							const {data:res} =await uni.$http.post('/blog/add',formdata);
							if(res.code=='0'){
								uni.showToast({
									title:'发布成功！'
								})
								this.clearInfo();
							}else if(res.code =='-1'){
			
								uni.showToast({
										title:res.message
								})
							}
						},
		},
	    onShow() {
	    
	    },

		onLoad:function(options){
			this.authorization=uni.getStorageSync('authorization');
			console.log('---options.classify1---',options.classify1);
			this.classify1=options.classify1;
			console.log('---this.classify1---',this.classify1);
			this.clearInfo(options.classify1);
		}
	}
</script>

<style lang="less">
		.write_wrap{
			margin-top: 50px;
			margin-left: 20rpx;
			margin-right: 20rpx;
			height: 1000rpx;
			//padding: 10rpx 40rpx 40rpx 40rpx;
		}
		.uploadImg{
			width: 100rpx;
			height: 100rpx;
		}
		.labelImg{
			width: 300rpx;
			height: 200rpx;
		}
		.adviseText{
			margin-top: 8rpx;
			font-size: 12px;
			color: #999aaa;
		}
		.title{
			margin-top: 20rpx;
			font-size: 38rpx;
			margin-bottom: 15rpx;
			font-weight: bold;
			font-family: 'Microsoft YaHei';
		}
</style>
